<?php defined('BASEPATH') or die("No access");
    class Category_article_model extends MY_Model
    {
        public function __construct()
        {
            parent::__construct();
        }
        //get all article
        public function getAll($limit,$offset,$status)
        {
            $this->db->order_by('category_article_order','asc');
            if($limit>0)
            {
                $this->db->limit($limit,$offset);
            }
            if($status==0)
            {
                $this->db->where('category_article_publish',0);
            }
            else
            {
                if($status>0)
                {
                   $this->db->where('category_article_publish',1); 
                }
            }
            $query=$this->db->get('category_article');
            return $query->result(); 
        }
        //get article by id 
        public function getCategoryarticleById($id)
        {
            $this->db->where('category_article_id',$id);
            $query=$this->db->get('category_article');
            return $query->row();
        }
        // insert or update 
        public function save($id,$data=array())
        {
            if($id>0)
            {
                //update new record
                $this->db->where('category_article_id',$id);
                $this->db->update('category_article',$data);
                
            }
            else
            {
                $this->db->insert('category_article',$data);
            }
            return $this->db->affected_rows();
        }
        //delete array articles
        public function delete($id=array())
        {
            $this->db->where_in('category_article_id',$id);
            $this->db->delete('category_article');
            return $this->db->affected_rows();
        }
        //save order
        public function saveorder($id=array(),$order=array())
        {
            for($i=0;$i<count($id);$i++)
            {
                $this->db->where('category_article_id',$id[$i]);
                $this->db->update('category_article',array('category_article_order'=>$order[$i]));
            }
        }
        //publish or unpublish
        public function publish($status,$id=array())
        {
            $this->db->where_in('category_article_id',$id);
            if($status==0)
            {
                $this->db->update('category_article',array('category_article_publish'=>0));
            }
            else
            {
                $this->db->update('category_article',array('category_article_publish'=>1));
            }
            return $this->db->affected_rows();
        }
    }
?>

